using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;

namespace Century.Model
{
    public partial class SystemConfiguration
    {
        //[Required]
        //[Key]
        //[DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Id { get; set; }

        public virtual string Owner { get; set; }

        public virtual string ConfigKey { get; set; }

        public virtual string ConfigValue { get; set; }

        public virtual string CodeValue { get; set; }

        public virtual byte[] ConfigTag { get; set; }

        public virtual string PossibleValues { get; set; }

        [Required]
        public virtual string ValueType { get; set; }

        public virtual string Description { get; set; }

        [Required]
        public virtual bool IsEncrypt { get; set; }

        public virtual string EncryptType { get; set; }

        [MaxLength(1)]
        public virtual string ListSeparator { get; set; }

        [ScriptIgnore]
        [NotMapped]
        public dynamic Value
        {
            get
            {
                switch (ValueType.ToLower())
                {
                    case "int":
                        int intOut = 0;
                        int.TryParse(ConfigValue, out intOut);
                        return intOut;
                    case "bool":
                        bool boolOut = false;
                        bool.TryParse(ConfigValue, out boolOut);
                        return boolOut;
                    case "decimal":
                        decimal decimalOut = 0;
                        decimal.TryParse(ConfigValue, out decimalOut);
                        return decimalOut;
                    case "datetime":
                        DateTime datetimeOut = DateTime.Now;
                        DateTime.TryParse(ConfigValue, out datetimeOut);
                        return datetimeOut;
                    case "char":
                        char charOut = ' ';
                        char.TryParse(ConfigValue, out charOut);
                        return charOut;
                    case "double":
                        double doubleOut = 0;
                        double.TryParse(ConfigValue, out doubleOut);
                        return doubleOut;
                    case "timespan":
                        TimeSpan timeSpaneOut = new TimeSpan();
                        TimeSpan.TryParse(ConfigValue, out timeSpaneOut);
                        return timeSpaneOut;
                    case "list":
                        char sepperator = ',';
                        if (!string.IsNullOrEmpty(this.ListSeparator))
                        {
                            sepperator = Convert.ToChar(this.ListSeparator);
                        }
                        return ConfigValue.Split(sepperator).ToList();
                    default:
                        return ConfigValue.Replace("\\n", "\n");
                }
            }
        }

    }
}
